package com.sport.facilities.booking.system;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.sport.facilities.booking.system.httptask.BasicImageTask;
import com.sport.facilities.booking.system.httptask.BasicTask;
import com.sport.facilities.booking.system.httptask.MakeBookingCheckTask;
import com.sport.facilities.booking.system.httptask.MakeBookingPageTask;
import com.sport.facilities.booking.system.httptask.OnTaskCompleted;
import com.sport.facilities.booking.system.model.Booking;
import com.sport.facilities.booking.system.model.BookingCheckResult;
import com.sport.facilities.booking.system.model.MakeBookFormModel;
import com.sport.facilities.booking.system.ui.DialogHelper;

public class MakeBookingActivity extends Activity implements
		OnItemSelectedListener {
	private static final String TAG = "MakeBookingActivity";
	private Spinner sp_activity = null;
	private Spinner sp_center = null;
	private Spinner sp_dt_from = null;
	private Spinner sp_dt_to = null;
	private Spinner sp_t_from = null;
	private Spinner sp_t_to = null;
	private EditText txt_captcha = null;
	private MakeBookFormModel bfm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.make_booking);

		sp_activity = (Spinner) findViewById(R.id.spinner_activity);
		sp_center = (Spinner) findViewById(R.id.spinner_centre);
		sp_dt_from = (Spinner) findViewById(R.id.spinner_date_from);
		sp_dt_to = (Spinner) findViewById(R.id.spinner_date_to);
		sp_t_from = (Spinner) findViewById(R.id.spinner_time_from);
		sp_t_to = (Spinner) findViewById(R.id.spinner_time_to);
		txt_captcha = (EditText) findViewById(R.id.text_captcha);

		fnInitBuzzLogic();
	}

	@Override
	public void onResume() {
		super.onResume();
		fnLoadBookingPage();
		txt_captcha.setText("");
	}

	public void reset(View view) {
		sp_activity.setSelection(0);
		sp_center.setSelection(0);
		sp_dt_from.setSelection(0);
		sp_dt_to.setSelection(0);
		sp_t_from.setSelection(0);
		sp_t_to.setSelection(0);
		txt_captcha.setText("");
	}


	
	public void submit(View view) {
		final MakeBookFormModel bfm = fnGetInputFromView();
		Log.d(TAG, "fnCheckBookPage()" + bfm);
		if (bfm == null) {
			return;
		}
		final MakeBookingActivity mbActivity = this;
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object s) {
				BookingCheckResult bcr = (BookingCheckResult)s;
				
				ArrayList<Booking> lst = bcr.getLst();
				if (lst.size() <= 0) {
					DialogHelper
							.getOkAlertDialog(mbActivity,
									"No record has been found.",
									"Please search again.").show();
					return;
				}
				Intent i = new Intent(MakeBookingActivity.this,
						MakeBookingListActivity.class);
				i.putExtra("bookingList", lst);
				i.putExtra("bookingFormModel", bcr.getBm());
				MakeBookingActivity.this.startActivity(i);
			}

			public void onTaskFailed(String errMessage) {
				Log.e(TAG, errMessage);
				fnLoadBookingCaptcha();
				DialogHelper.getOkAlertDialog(mbActivity,
						"Error In Checking Booking", errMessage).show();

				txt_captcha.setText(""); //remove the old captcha when error 
			}
		};

		MakeBookingCheckTask bmt = new MakeBookingCheckTask(this,
				completeAction);
		bmt.fnCheckBooking(bfm);
	}

	private void fnLoadBookingPage() {
		fnSetBtnCheckEnabled(false);
		final MakeBookingActivity mbActivity = this;
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object s) {
				if (s != null) {
					fnLoadBookingCaptcha();
					fnGetFormData((MakeBookFormModel) s);
				} else {
					DialogHelper.getOkAlertDialog(mbActivity,
							"Error In Loading Booking Page",
							"Incorrect form data from server.").show();
				}
			}

			public void onTaskFailed(String errMessage) {
				Log.e(TAG, errMessage);
				DialogHelper.getOkAlertDialog(mbActivity,
						"Error In Loading Booking Page", errMessage).show();
				
			}
		};
		MakeBookingPageTask bpt = new MakeBookingPageTask(this, completeAction);
		bpt.fnLoadBookingPage();
	}

	private void fnGetFormData(MakeBookFormModel bfm) {
		this.bfm = bfm;

		ArrayAdapter<String> adpActivity = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, bfm.getLst_activity());
		sp_activity.setAdapter(adpActivity);

		ArrayAdapter<String> adpCenter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, bfm.getLst_centre());
		sp_center.setAdapter(adpCenter);
		sp_center.setSelection(0);

		ArrayAdapter<String> adpDtFrom = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, bfm.getLst_st_day());
		sp_dt_from.setAdapter(adpDtFrom);
		sp_dt_from.setSelection(1);

		ArrayAdapter<String> adpDtTo = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, bfm.getLst_ed_day());
		sp_dt_to.setAdapter(adpDtTo);
		sp_dt_to.setSelection(adpDtTo.getCount() - 8);

		ArrayAdapter<String> adpTimeFrom = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, bfm.getLst_st_time());
		sp_t_from.setAdapter(adpTimeFrom);
		sp_t_from.setSelection(1);

		ArrayAdapter<String> adpTimeTo = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, bfm.getLst_st_time());
		sp_t_to.setAdapter(adpTimeTo);
		sp_t_to.setSelection(adpTimeTo.getCount() - 1);
	}

	private void fnLoadBookingCaptcha() {
		final MakeBookingActivity mbActivity = this;
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object s) {
				ImageView iv = (ImageView) findViewById(R.id.captcha);
				iv.setImageBitmap((Bitmap) s);
				fnSetBtnCheckEnabled(true);
			}

			public void onTaskFailed(String errMessage) {
				Log.e(TAG, errMessage);
				DialogHelper.getOkAlertDialog(mbActivity,
						"Error In Loading Captcha", errMessage).show();
			}
		};
		BasicImageTask bit = new BasicImageTask(this, completeAction);
		bit.execute(BasicTask.PATH_BOOK_IMG);
	}

	private MakeBookFormModel fnGetInputFromView() {
		if (this.bfm == null) {
			return null;
		}

		if (sp_activity.getSelectedItem().toString() == ""
				|| sp_center.getSelectedItem().toString() == "") {
			DialogHelper.getOkAlertDialog(this, "Please fill in the data.",
					"Please select an Activity or Centre.").show();
			return null;
		}

		if (sp_dt_from.getSelectedItem().toString() == ""
				|| sp_dt_to.getSelectedItem().toString() == ""
				|| sp_dt_to.getSelectedItem().toString() == ""
				|| sp_t_from.getSelectedItem().toString() == "") {
			DialogHelper.getOkAlertDialog(this, "Please fill in the data.",
					"Please select the check date time peroid.").show();
			return null;
		}

		if (txt_captcha.getText().toString().equalsIgnoreCase("")
				|| txt_captcha.getText().length() <= 3) {
			DialogHelper.getOkAlertDialog(this, "Please fill in the data.",
					"Please fill in the captcha value.").show();
			return null;
		}

		bfm.setActivity(sp_activity.getSelectedItem().toString());
		bfm.setCentre(sp_center.getSelectedItem().toString());
		bfm.setSt_day(sp_dt_from.getSelectedItem().toString());
		bfm.setEd_day(sp_dt_to.getSelectedItem().toString());
		bfm.setSt_time(sp_t_from.getSelectedItem().toString());
		bfm.setEd_time(sp_t_to.getSelectedItem().toString());
		bfm.setKaptchaValue(txt_captcha.getText().toString());
		return bfm;
	}

	private void fnSetBtnCheckEnabled(boolean b) {
		Button bnCheck = (Button) findViewById(R.id.btn_check);
		bnCheck.setEnabled(b);
	}

	private void fnInitBuzzLogic() {
		// When Activity changes, the Center will be changed too.
		sp_activity.setOnItemSelectedListener(this);
	}

	// OnItemSelectedListener
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		// sp_center
	}

	// OnItemSelectedListener
	public void onNothingSelected(AdapterView<?> parent) {

	}
}
